﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;

public partial class User_PurchaseCardReg_CardPersonal : System.Web.UI.Page
{
    UserBean bllUser = new UserBean();
    WebUtility utility = new WebUtility();
    AccountsCharge bllAccount = new AccountsCharge();
    TransCardPoints2UserAccount bllTransCard = new TransCardPoints2UserAccount();
    ReadParamtes bllParam = new ReadParamtes();
    SendSMS bllSMS = new SendSMS();
    ConsultationCardPwdSelect bllCardInfo = new ConsultationCardPwdSelect();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.txt_Birth.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd'})");
        if (!IsPostBack)
        {
            WebUtility.BindList("行业属性", ddl_Vocation);
            WebUtility.BindList("文化程度代码", ddl_Education);
        }
    }

    protected void submit_Click(object sender, ImageClickEventArgs e)
    {
        string cardNO = Request.QueryString["CardNo"].ToString();
        string cardPwd = Request.QueryString["CardPwd"].ToString();

        string guid = Guid.NewGuid().ToString();
        string accountNO = this.MobileNO.Text;
        string password = this.password.Text;
        string name = this.realName.Text;
        string sex = this.rb_Man.Checked ? this.rb_Man.Text : this.rb_Womam.Text;
        string tel = this.phone.Text;
        string email = this.email.Text;
        string vocation = this.ddl_Vocation.SelectedIndex != 0 ? this.ddl_Vocation.SelectedItem.Text : ""; ;
        string address = this.txt_Address.Text;
        string zipCode = this.zipCode.Text;
        string education = this.ddl_Education.SelectedIndex != 0 ? this.ddl_Education.SelectedItem.Text : "";
        string birth = this.txt_Birth.Text;
        string like = this.txt_Like.Text;

        bool flag = bllUser.IsExistsUserInfo(accountNO);
        if (flag == true)
        {
            utility.AlertMsg("您的帐户已经被占用！");
            return;
        }
        else
        {
            bool isRequestTimeOut = bllCardInfo.IsRequestTimedOut(accountNO, password);
            if (isRequestTimeOut == false)
            {
                utility.AlertMsg("对不起，您所输入的密码已过期！请重新申请。");
                return;
            }
            else
            {
                bool isSame = bllCardInfo.IsSameToVerifyCodeAndMobileNO(accountNO, password);
                if (isSame == false)
                {
                    utility.AlertMsg("对不起，您所输入的验证码错误！请重新输入。");
                    return;
                }
                else
                {
                    //取得注册协议名称
                    string protocolName = "";
                    DataTable dtProtocol = bllParam.getParamInfo("RegProtocol");
                    for (int i = 0; i < dtProtocol.Rows.Count; i++)
                    {
                        protocolName += dtProtocol.Rows[i]["ParameterValue"].ToString() + ",";
                    }

                    int index = bllAccount.AddChargeAccount(guid, accountNO, 0, "", password, protocolName.Trim(','), "1", 1);

                    if (index > 0)
                    {
                        int count = bllUser.AddContact_PurchaseCard(guid, accountNO, name, sex, tel, email, vocation, address, zipCode, education, birth, like);

                        if (count > 0)
                        {
                            int index1 = bllTransCard.transCardPoints(cardNO, cardPwd, accountNO);

                            //取得用户帐户点数
                            string cardPoints = "";
                            DataTable dtAccountInfo = bllAccount.GetAccountInfo(accountNO);
                            cardPoints = dtAccountInfo.Rows[0]["U_AccountPoints"].ToString();

                            //取得购卡用户注册成功后的发送短信文本
                            string regMsgTemp = bllParam.getParamValue("SmsPurchaseReg");
                            string regMsg = WebUtility.GenrateSendPurchaseRegMsg(accountNO, password, cardPoints, regMsgTemp);

                            if (index1 == 1)
                            {
                                bllSMS.SendMsg(regMsg, accountNO, "1");
                                utility.AlertMsg("注册成功！,系统稍后将会以短信方式把用户名和密码发送到你的注册手机上");
                                Response.Redirect(WebUtility .getWebRootPath() + "/Login/Login.aspx");
                                return;
                            }
                            else
                            {
                                utility.AlertMsg("注册失败！");
                                return;
                            }
                        }
                    }
                    else
                    {
                        utility.AlertMsg("注册失败！");
                        return;
                    }
                }
            }
        }
    }
}
